package cn.edu.zzuli.wechat.user.service.impl;

import cn.edu.zzuli.model.dto.StateAndLockDto;
import cn.edu.zzuli.model.entity.OfferOrder;
import cn.edu.zzuli.model.entity.RunOrder;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.wechat.user.mapper.OfferOrderMapper;
import cn.edu.zzuli.wechat.user.mapper.RunOrderMapper;
import cn.edu.zzuli.wechat.user.mapper.UserMapper;
import cn.edu.zzuli.wechat.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RunOrderMapper runOrderMapper;

    @Autowired
    private OfferOrderMapper offerOrderMapper;

    @Override
    public Map<String, Object> getInfo(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        int runCount = runOrderMapper.selectCount(new QueryWrapper<RunOrder>().eq("ruid",uid));
        int offerCount = offerOrderMapper.selectCount(new QueryWrapper<OfferOrder>().eq("ouid",uid));
        Double wealth = baseMapper.getUserWealth(uid);
        Integer userCredit = baseMapper.getUserCredit(uid);
        map.put("runCount",runCount);
        map.put("offerCount",offerCount);
        map.put("wealth",wealth);
        map.put("credit",userCredit);
        return map;
    }

    @Override
    public StateAndLockDto getStateAndLock(Integer uid) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("state","locked","uid").eq("uid",uid);
        User user = baseMapper.selectOne(wrapper);
        return new StateAndLockDto(user.getState(),user.getLocked(),user.getUid());
    }

    @Override
    public void updateWealth(Integer uid, Double bounty) {
        baseMapper.updateWealth(uid,bounty);
    }

    @Override
    public Double getWealth(Integer uid) {
        return baseMapper.getUserWealth(uid);
    }
}
